/* Gannet OS - Serial Functions for Kernel
 
 This file is the serial functions for the kernel to allow it to dump to serial

 This is the C Code file (see serial.h for the header)

 This is part of the Gannet OS, For copy right and licensing see LICENSE in the
 root folder, or visit http://gannet.org.uk/license/

 Actually, these functions come verbatim from http://wiki.osdev.org/Serial_Ports
 credit where credit is due!
*/

#include "serial.h"
#include "io.h"

#define PORT 0x3f8   /* COM1 - magic number */
 
void serial_initialise() {
   outb(PORT + 1, 0x00);    // Disable all interrupts
   outb(PORT + 3, 0x80);    // Enable DLAB (set baud rate divisor)
   outb(PORT + 0, 0x03);    // Set divisor to 3 (lo byte) 38400 baud
   outb(PORT + 1, 0x00);    //                  (hi byte)
   outb(PORT + 3, 0x03);    // 8 bits, no parity, one stop bit
   outb(PORT + 2, 0xC7);    // Enable FIFO, clear them, with 14-byte threshold
   outb(PORT + 4, 0x0B);    // IRQs enabled, RTS/DSR set
}

int _serial_is_transmit_empty() {
   return inb(PORT + 5) & 0x20;
}
 
void serial_write(char a) {
   while (_serial_is_transmit_empty() == 0);
 
   outb(PORT,a);
}

void serial_write_string(char* a){
	while(*a != 0x00) {
		serial_write(*a);
		a++;
	}
}